
//   顶部展示栏配置
//   路由点击全局处理
export default class route {
    constructor() {
      const Vue_ = arguments[0].prototype;
      Vue_.$setRouteTitle = this.setRouteTitle;
      Vue_.$toRoutePath = this.toRoutePath;
      Vue_.$returnClass = this.returnClass;
      Vue_.$filterParam = this.filterParam;
      Vue_.$blackRoute = this.blackRoute;
      Vue_.$changeNavlist = this.changeNavlist;
      Vue_.$findNavList = this.findNavList;
      Vue_.$changeOpeneds = this.changeOpeneds;
      Vue_.$clearNavList = this.clearNavList;
    }
  
  
    /**
     * 过滤空字符串字段
     */
    filterParam(param) {
      let post_data = {};
      for (let val in param) {
        if (param[val] !== '') {
          post_data[val] = param[val];
        }
      }
      return post_data;
  
    }
  
    /**
     * 
     *   @param {object} pathObj 
     *   pathObj.edit    是否修改顶部栏    true  or  false
     *   pathObj.path    跳转路由地址
     *   
     *    example: { path: "/facilitator_list/facilitator_list_add",edit:true,query:{a:1}
     */
    toRoutePath(pathObj) {
      if (pathObj && typeof pathObj === "object") {
        if (pathObj.edit) {
  
        }
        this.$router.push({ path: pathObj.path, query: pathObj.query || {} });
      } else {
        throw "请传入对象 object"
      }
    }
    returnClass(type) {
      switch (type) {
        case 1: return { title: "物流企业管理", path: "/" }; break
        case 2: return { title: "物流信息管理", path: "/" }; break
        case 3: return { title: "信息互助管理", path: "/" }; break
        case 4: return { title: "人员管理", path: "/" }; break
        case 5: return { title: "系统管理", path: "/" }; break
      }
    }
    setRouteTitle() {
      let terrace = this.$returnClass(this.$route.meta.type);       //  归属功能
      let route_list = this.$route.meta.route;                         //  子路由含功能路由
      let nav_titleList = [                                           //  顶部分级展示栏   首页/**/
        {
          title: "首页",
          path: "/"
        }
      ];
      nav_titleList.push(terrace);
      this.$store.commit('changeState', { nav_titleList: nav_titleList.concat(route_list) })
      let showNavLists = () => {
        let list = this.$store.state.showNavList || [];
        list.forEach((v, k) => {
          v.edit = false;
        });
        return list;
      }
  
      let path_ = route_list[route_list.length - 1]['path'],
        title_ = route_list[route_list.length - 1]['title'],
        query_ = this.$route.query,
        oldNavLists = showNavLists();
  
      let flag = 0;     //用于表示新的对象是否插入了数组
  
  
      let newShowNavList = {
        title: title_,
        path: path_,
        edit: true,
        query: query_,
        class: ["nav-Toview-item"],
        class1: ["activeColor", "nav-Toview-item"],
        index: 0,
        show: true,
      }
      oldNavLists.forEach((v, k) => {
        if (v.path == path_) {
          oldNavLists.splice(k, 1, newShowNavList);
          flag = 1;
        }
      });
      if (flag == 0) {          //还未替换的，那么就插入到最后吧
        oldNavLists = oldNavLists.concat(newShowNavList);
      }
      this.$store.commit('changeState', { showNavList: oldNavLists })
    }
  
    /**
     * 
     *  撤回-返回上一个页面
     * 
     */
    blackRoute(obj) {
      let showNavList = this.$store.state.showNavList,
        nav_titleList = this.$store.state.nav_titleList,
        route_path = this.$route.path || '/',
        route_list = this.$route.meta.route || [],
        terrace = this.$returnClass(this.$route.meta.type);       //  归属功能
      this.$toRoutePath({
        path: obj.path,
        edit: true,
        query: obj.query || {}
      });
      let path_obj = null;                       //  判断当前页面数组是否存在-不存在就新增标题栏
      for (let i = 0; i < showNavList.length; i++) {
        if (showNavList[i].path == obj.path) {
          showNavList[i].edit = true;
          path_obj = showNavList[i];
        } else {
        }
  
        if (showNavList[i].path == route_path) {
          showNavList.splice(i, 1);
        }
      }
      //  返回路由已关闭-重新追加路由地址
      this.$findNavList(obj.path, (data) => {
        this.$changeOpeneds(data);
        if (path_obj == null) {
          showNavList.push(
            {
              title: data.title,
              path: data.route,
              edit: true,
              class: ["nav-Toview-item"],
              class1: ["activeColor", "nav-Toview-item"],
              index: data.index,
              // obj: arguments[1],
              // id_: null
            }
          )
        } else {
  
        }
      })
      this.$changeNavlist(obj.path)
      let new_titleLists = [{ title: "首页", path: "/" }, terrace].concat(this.$route.meta.route);
      this.$store.commit("changeState", { nav_titleList: new_titleLists });
      localStorage.setItem('showNav', JSON.stringify(showNavList))
    }
    // 修改左侧导航栏
    changeNavlist(path) {
      let navList = this.$store.state.navList;
      if (path) {
        let fn = list => {
          list.forEach((v, k) => {
            if (v.list && Array.isArray(v.list)) {
              fn(v.list);
            } else {
              if (v.route == path) {
                v.edit = true;
              } else {
                v.edit = false;
              }
            }
          });
        };
        fn(navList);
      }
    }
    findNavList(path, callback) {
      let navList = this.$store.state.navList;
      if (path) {
        let fn = list => {
          list.forEach((v, k) => {
            if (v.route == path) {
              callback(v)
            } else
              if (v.list && Array.isArray(v.list)) {
                fn(v.list);
              }
          });
        };
        fn(navList);
      }
    }
    changeOpeneds(data) {
      let index_ = data.index.split("-")[0] || '1', openedsList = this.$store.state.openedsList || [];
      openedsList[0] = index_;
      this.$store.commit("changeState", { openedsList: openedsList });
    }
    // 情况菜单栏全部选项
    clearNavList() {
      let fn = list => {
        list.forEach((v, k) => {
          if (v.list && Array.isArray(v.list)) {
            fn(v.list);
          } else {
            v.edit = false;
          }
        });
      };
      fn(this.$store.state.navMap || []);
    }
  }